package com.bbs.crawl;

import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.bbs.bean.BBSOfBitAuto;
import com.bbs.dao.BBSDaoOfBitAuto;
import com.car.util.DocUtil;
import com.car.util.Util;

public class BBSCrawlOfBitAuto implements Runnable {
	static Logger logger = Logger.getLogger(BBSCrawlOfBitAuto.class.getName());
	private BBSDaoOfBitAuto dao;
	private String urlSpell;
	int count = 0;

	public BBSCrawlOfBitAuto(BBSDaoOfBitAuto dao, String urlSpell) {
		super();
		this.dao = dao;
		this.urlSpell = urlSpell;
	}

	public void run() {
		getUrls(this.urlSpell);
	}

	// 入口
	public void getUrls(String urlSpell) {
		String url = "http://baa.bitauto.com/" + urlSpell + "/index-all-all-1-0.html";
		// int page = getBBSPages(url);
		int page = 1;
		if (page == 0) {
			// return;
		}
		logger.info(urlSpell + "开始");
		int bbsid = dao.urlSpell2ID(urlSpell);
		if (bbsid != 0) {
			dao.createTable(bbsid);
		} else {
			System.out.println(bbsid + "\t" + urlSpell + "\t" + "bbsid=0");
			return;
		}
		for (int i = 1; i < page + 1; i++) {
			String bbsPageUrl = "http://baa.bitauto.com/" + urlSpell + "/index-all-all-" + i + "-0.html";
			Document contentPage = DocUtil.getDoc(bbsPageUrl);
			Elements liPage = null;
			try {
				liPage = contentPage.select("div.postslist_xh li.bt a");
			} catch (Exception e1) {
				break;
			}
			for (Element e : liPage) {
				String bbsUrl = e.attr("href");
				startCrawl(bbsUrl, bbsid);
			}
		}
		dao.updateBBSTime("bitauto_ltgxsj", urlSpell);
		logger.info(urlSpell + "结束" + this.count);
	}

	public int getBBSPages(String url) {
		int page = 1;
		try {
			Document doc = DocUtil.getDoc(url);
			Elements pages = doc.select("div.the_pages a");
			int pageSize = pages.size();
			Element maxPage = pages.get(pageSize - 2);
			page = Integer.parseInt(maxPage.text());
		} catch (Exception e) {
			page = 0;
		}
		return page;
	}

	public void startCrawl(String url, int bbsid) {
		if (url.contains("announcement")) {
			return;
		}
		Document doc = DocUtil.getDoc(url);
		try {
			int bbs_bid = Integer.parseInt(Util.getMatched(url, "(.*-)(\\d{3,})(\\.html)").group(2));
			String bbs_bt = doc.select("div.title_box h1").get(0).text();
			int bbs_sfwd = url.contains("ask") ? 1 : 0;
			int bbs_sfjh = doc.select("div.jinghuazhang") == null ? 0 : 1;
			String bbs_nr = "";
			Elements nr = doc.select("div.post_width p");
			StringBuffer sb = new StringBuffer();
			for (Element e : nr) {
				if (e.text().trim().length() != 0 && e.text() != null) {
					sb.append(e.text().trim() + "\n");
				}
			}
			bbs_nr = sb.toString();
			String bbs_ftsj = doc.select("div.time_box>span").get(0).text().substring(3);
			String hit = doc.select("div.title_box").get(0).select("span").text();
			int bbs_djs = Integer.parseInt(hit.split("/")[1]);
			int bbs_hfs = Integer.parseInt(hit.split("/")[0]);
			String yhid = doc.select("div.user_name a.mingzi").get(0).attr("href").split("u")[1];
			int bbs_yhid = Integer.parseInt(yhid.substring(0, yhid.length() - 1));
			Elements userInfo = doc.select("div.post_fist").select("div.user_info li");
			String bbs_yhdj = "";
			int bbs_tzs = 0;
			int bbs_tzjh = 0;
			String bbs_dq = "";
			String bbs_zc = "2000-01-01";
			if (userInfo == null || userInfo.size() == 0) {
				bbs_yhdj = userInfo.get(0).select("span").text();
				bbs_tzs = Integer.parseInt(userInfo.get(1).select("span a").text());
				bbs_tzjh = Integer.parseInt(userInfo.get(1).select("span").text().split("\\(")[1].split("精华")[0]);
				bbs_dq = userInfo.get(2).select("span").text();
				bbs_zc = userInfo.get(4).select("span").text();
			}
			BBSOfBitAuto b = new BBSOfBitAuto(bbs_bid, bbs_bt, bbs_sfjh, bbs_sfwd, bbs_nr, bbs_ftsj, bbs_hfs, bbs_djs,
					bbs_yhid, bbs_yhdj, bbs_tzs, bbs_tzjh, bbs_dq, bbs_zc);
			dao.insertBBS(b, bbsid);
			count++;
		} catch (IndexOutOfBoundsException e) {
		} catch (Exception e2) {
			System.out.println(url);
			e2.printStackTrace();
		}

	}

}
